<template>
  <transition name="dialog-fade">
    <el-dialog
      v-if="roleDialog"
      :title="dialogTitle"
      class="dialog-addrole"
      :visible.sync="roleDialog"
      width="50%"
      append-to-body
      @close="closeDialog(0)"
    >
      <el-form ref="formInfo" :model="formInfo" class="demo-form-inline" label-width="100px">
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="formInfo.rolename"></el-input>
          <!-- <el-select v-model="value" style="width:98%" @change="rolecange" placeholder="请选择角色名称">
            <el-option
              v-for="(item,index) in formInfo"
              :label="item.name"
              :value="item.id"
              :key="index"
            ></el-option>
          </el-select>-->
        </el-form-item>
        <div class="infoswitch" v-for="(item,index) in roleuserlist" :key="index">
          <div class="centerswitch">
            <el-switch
              v-model="item.checked"
              @change="switchbox(item)"
              :active-text="item.pc_menu_name"
              active-color="#409EFF"
              inactive-color="#DCDFE6"
            ></el-switch>
          </div>
          <div class="swit_user" v-for="(items,indexs) in item.list" :key="indexs">
            <el-switch
              class="swit_item"
              :disabled="!item.checked"
              @change="switchboxs(items)"
              v-model="items.checked"
              :active-text="items.pc_menu_name"
              active-color="#409EFF"
              inactive-color="#DCDFE6"
            ></el-switch>
            <div class="swit_usersd">
              <el-switch
                v-for="(itemes,indexes) in items.list"
                :key="indexes"
                class="swit_item"
                :disabled="!items.checked"
                v-model="itemes.checked"
                :active-text="itemes.pc_menu_name"
                active-color="#409EFF"
                inactive-color="#DCDFE6"
              ></el-switch>
            </div>
          </div>
        </div>
        <el-form-item style="text-align: right;">
          <el-button @click="closeDialog(0)">取消</el-button>
          <el-button type="primary" @click="submitForm('formInfo')">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </transition>
</template>

<script>
import { rolelist, edits, manage, addResult, addRole } from "@/api/user";
import Base64 from "@/utils/base";
export default {
  name: "DialogAddrole",
  inject: ["reload"],
  props: {
    dialogTitle: {
      type: String,
      default: ""
    },
    itemInfo: {
      type: Object,
      default: function() {
        return {};
      }
    }
  },
  data() {
    return {
      arrid: "",
      id: "",
      roleDialog: false,
      formInfo: {
        rolename: ""
      },
      uservalue: 1 ? false : true,
      value: "",
      roleuserlist: "",
      name: ""
    };
  },
  created() {
    // this.getrolelist();
    this.getaddResult();
    // this.getedits()
  },
  mounted() {},
  methods: {
    switchbox(e) {
      this.roleuserlist.map(v => {
        if (e.id == v.id) {
          v.list.map(i => {
            i.checked = e.checked;
            i.list.map(s => {
              s.checked = e.checked;
            });
          });
        }
      });
      // this.parentcurr = e
    },
    switchboxs(e) {
      this.roleuserlist.map(v => {
        v.list.map(i => {
          if (e.id == i.id) {
            i.list.map(s => {
              s.checked = e.checked;
            });
          }
        });
      });
    },
    // rolecange(e){
    //   console.log(e)
    //   this.id = e
    //   this.formInfo.map(v=>{
    //     if(e==v.id){
    //       console.log(v.name)
    //       this.name = v.name
    //     }
    //   })
    // },
    // getrolelist() {
    //   manage().then(res => {
    //     console.log(res);
    //     this.formInfo = res.result;
    //   })
    // },
    getaddResult() {
      addResult().then(res => {
        this.roleuserlist = res.result.data;
        this.roleuserlist.map(s=>{
          s.list.map((v,index)=>{
            if(v.id==109){
              s.list.splice(index, 1)
            }
          })
        })
      });
    },
    // 获取角色信息列表
    // getedits() {
    //   edits({
    //         request_data: Base64.encode(JSON.stringify({id:1}))
    //       }).then(res => {
    //     console.log(res)
    //     this.roleuserlist = res.data
    //     console.log(this.roleuserlist)
    //   })
    // },
    // 获取下拉框
    // getOpt() {
    //   this.getRequest('/asset/getTypeList', {}).then(res => {
    //     this.options = res.obj
    //   })
    // },
    // 保存操作
    submitForm(formName) {
      let arr = [];
      let arrs = [];
      let arrss = [];
      this.roleuserlist.map(v => {
        if (v.checked == true) {
          arr.push(v.id);
        }
        v.list.map(i => {
          if (i.checked == true) {
            arrs.push(i.id);
          }
          i.list.map(s => {
            if (s.checked == true) {
              arrss.push(s.id);
            }
          });
        });
      });
      var newArr = [];
      for (var i = 0; i < arr.concat(arrs, arrss).length; i++) {
        if (newArr.indexOf(arr.concat(arrs, arrss)[i]) == -1) {
          newArr.push(arr.concat(arrs, arrss)[i]);
        }
      }
      let parems = {
        name: this.formInfo.rolename,
        ids: newArr.join(",")
      };
      if(this.formInfo.rolename==""){
        this.$message({
          message:"请输入角色名称",
          type:"warning"
        })
        return
      }
      addRole({
        request_data: Base64.encode(JSON.stringify(parems))
      }).then(res => {
        if(res.status==2){
          this.$message({
            message:res.message,
            type:'warning'
          })
          return
        }
        this.roleDialog = false;
        this.reload();
      });
    },
    // 关闭弹框
    closeDialog(flag) {
      this.$refs["formInfo"].resetFields();
      this.roleDialog = false;
      this.$emit("closeDialog", flag);
    }
  }
};
</script>

<style scoped lang="scss">
.dialog-fade-enter-active {
  -webkit-animation: dialog-fade-in 0.3s;
  animation: dialog-fade-in 0.3s;
}
.dialog-fade-leave-active {
  -webkit-animation: dialog-fade-out 0.3s;
  animation: dialog-fade-out 0.3s;
}
@-webkit-keyframes dialog-fade-in {
  0% {
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
  100% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}
@keyframes dialog-fade-in {
  0% {
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
  100% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
}
@-webkit-keyframes dialog-fade-out {
  0% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
}
@keyframes dialog-fade-out {
  0% {
    -webkit-transform: translate3d(0, 0, 0);
    transform: translate3d(0, 0, 0);
    opacity: 1;
  }
  100% {
    -webkit-transform: translate3d(0, -20px, 0);
    transform: translate3d(0, -20px, 0);
    opacity: 0;
  }
}
.infoswitch {
  width: 94%;
  height: auto;
  background: #ffffff;
  box-shadow: 0px 0px 10px 0px rgba(55, 55, 55, 0.17);
  border-radius: 10px;
  margin-left: 50%;
  margin-bottom: 30px;
  transform: translateX(-50%);
  overflow: hidden;
  //   display: flex;
  //   justify-content: center;
}
.centerswitch {
  // width: 90%;
  margin-left: 34px;
  margin-top: 20px;
  margin-bottom: 20px;
}
.swit_user {
  margin-left: 56px;
  flex-wrap: wrap;
}
.swit_useruser {
  margin-left: 56px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.swit_users {
  margin-left: 76px;
  margin-bottom: 20px;
}
.swit_usersd {
  margin-left: 50px;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
}
.swit_item {
  margin-bottom: 20px;
  margin-right: 20px;
  margin-left: 20px;
}
</style>
